﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using UnityEditor;
    using UnityEngine;

    internal class VariableManager
    {
        private static bool autoScroll;
        private List<FsmVariable> fsmVariables = new List<FsmVariable>();
        private List<NamedVariable> globalVariables = new List<NamedVariable>();
        private List<FsmInfo> globalVariablesInfo = new List<FsmInfo>();
        private bool listIsDirty;
        private const float NameColumnWidth = 155f;
        private string newVariableName = "";
        private VariableType newVariableType;
        private Vector2 scrollPosition;
        private static float scrollViewHeight;
        private int selectedIndex = -1;
        private static Rect selectedRect;
        private string selectedVariableName = "";
        private bool sortByVariableType;
        private readonly List<FsmVariable> unusedVariables = new List<FsmVariable>();
        private readonly Dictionary<NamedVariable, List<FsmInfo>> usage = new Dictionary<NamedVariable, List<FsmInfo>>();
        private readonly Dictionary<NamedVariable, int> usageCount = new Dictionary<NamedVariable, int>();

        private static void AddVariable(VariableType type, string name, [Optional, DefaultParameterValue(true)] bool undo)
        {
            if (undo)
            {
                FsmEditor.RegisterUndo("Add Variable");
            }
            FsmVariable.AddVariable(FsmEditor.SelectedFsm.Variables, type, name);
            FsmEditor.SetFsmDirty(true, false);
        }

        public void BuildFsmVariableList()
        {
            if (FsmEditor.SelectedFsm != null)
            {
                this.fsmVariables = FsmVariable.GetFsmVariableList(FsmEditor.SelectedFsm.Variables, FsmEditor.SelectedFsm.Owner);
                this.SortVariableList();
                this.listIsDirty = false;
            }
        }

        private void ChangeVariableType(FsmVariable fsmVariable, VariableType newType)
        {
            if (this.CheckDeleteVariable("Edit Variable Type", "This variable is used! Are you sure you want to change its type?", fsmVariable))
            {
                FsmEditor.RegisterUndo("Edit Variable");
                string name = fsmVariable.Name;
                this.DeleteVariable(fsmVariable, false, false);
                AddVariable(newType, name, false);
                FsmBuilder.RemoveVariableUsage(fsmVariable.NamedVar);
                this.listIsDirty = true;
                FsmEditor.SetFsmDirty(true, false);
            }
        }

        private bool CheckDeleteVariable(string title, string warning, FsmVariable fsmVariable)
        {
            this.UpdateUseCount();
            if (!this.unusedVariables.Contains(fsmVariable))
            {
                return EditorUtility.DisplayDialog(title, warning, "Yes", "No");
            }
            return true;
        }

        private void CheckForNetworkSyncErrors()
        {
            NetworkView networkView = FsmEditor.SelectedFsmGameObject.networkView;
            if (networkView == null)
            {
                if (GUILayout.Button("GameObject requires a NetworkView component.\nClick to fix.", FsmEditorStyles.ErrorBox, new GUILayoutOption[0]))
                {
                    FsmEditor.SelectedFsmGameObject.AddComponent<NetworkView>().observed = FsmEditor.SelectedFsmComponent;
                }
            }
            else if ((networkView.observed != FsmEditor.SelectedFsmComponent) && GUILayout.Button("GameObject NetworkView component must Observe this PlayMakerFSM. Drag the PlayMakerFSM into the Observed field, or click this box to fix.", FsmEditorStyles.ErrorBox, new GUILayoutOption[0]))
            {
                networkView.observed = FsmEditor.SelectedFsmComponent;
            }
        }

        private void DeleteUnusedVariables()
        {
            this.UpdateUseCount();
            if (this.unusedVariables.Count == 0)
            {
                EditorUtility.DisplayDialog("Delete Unused Variables", "No unused variables...", "OK");
            }
            else if (EditorUtility.DisplayDialog("Delete Unused Variables", "Are you sure you want to delete " + this.unusedVariables.Count + " unused variables?", "Yes", "No"))
            {
                FsmEditor.RegisterUndo("Delete Unused Variables");
                foreach (FsmVariable variable in this.unusedVariables)
                {
                    this.DeleteVariable(variable, false, false);
                }
                this.Reset();
            }
        }

        private void DeleteVariable(FsmVariable fsmVariable, [Optional, DefaultParameterValue(true)] bool undo, [Optional, DefaultParameterValue(true)] bool checkDialog)
        {
            if (!checkDialog || this.CheckDeleteVariable("Delete Variable", "This variable is used! Are you sure you want to delete it?", fsmVariable))
            {
                if (undo)
                {
                    FsmEditor.RegisterUndo("Delete Variable");
                }
                FsmBuilder.RemoveVariableUsage(fsmVariable.NamedVar);
                FsmVariable.DeleteVariable(FsmEditor.SelectedFsm.Variables, fsmVariable);
                this.listIsDirty = true;
                FsmEditor.SetFsmDirty(true, false);
            }
        }

        public void Deselect()
        {
            FsmEditor.ResetKeyboard();
            this.selectedVariableName = "";
            this.newVariableName = "";
            this.selectedIndex = -1;
            FsmEditor.Repaint(false);
        }

        private void DoAutoScroll()
        {
            if ((this.selectedVariableName != "") && ((Event.current.type == EventType.Repaint) && autoScroll))
            {
                scrollViewHeight = GUILayoutUtility.GetLastRect().height;
                if (selectedRect.y < 0f)
                {
                    this.scrollPosition.y += selectedRect.y;
                    FsmEditor.Repaint(false);
                }
                else if ((selectedRect.y + selectedRect.height) > scrollViewHeight)
                {
                    this.scrollPosition.y += (selectedRect.y + selectedRect.height) - scrollViewHeight;
                    FsmEditor.Repaint(false);
                }
                autoScroll = false;
            }
        }

        private void DoGlobalVariablesTable()
        {
            if (this.globalVariables.Count != 0)
            {
                GUILayout.Space(10f);
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.BeginHorizontal(FsmEditorStyles.TableRowBox, new GUILayoutOption[0]);
                GUILayout.Label(new GUIContent("Globals", "Global variables used by this FSM."), new GUILayoutOption[0]);
                GUILayout.FlexibleSpace();
                GUILayout.Label(new GUIContent("Used", "The number of times the Variable is used in the FSM."), new GUILayoutOption[0]);
                GUILayout.EndHorizontal();
                foreach (NamedVariable variable in this.globalVariables)
                {
                    int num;
                    GUILayout.BeginHorizontal(FsmEditorStyles.TableRowBox, new GUILayoutOption[0]);
                    GUIStyle tableRowText = FsmEditorStyles.TableRowText;
                    if (GUILayout.Button(new GUIContent(variable.Name, variable.Tooltip), tableRowText, new GUILayoutOption[] { GUILayout.MinWidth(155f) }))
                    {
                        FsmEditor.ResetKeyboard();
                        this.selectedVariableName = "";
                        this.newVariableName = this.selectedVariableName;
                        if ((Event.current.button == 1) || EditorGUI.actionKey)
                        {
                            this.GenerateStateList(variable).ShowAsContext();
                        }
                    }
                    this.usageCount.TryGetValue(variable, out num);
                    GUILayout.FlexibleSpace();
                    GUILayout.Label(num.ToString(CultureInfo.CurrentCulture), tableRowText, new GUILayoutOption[0]);
                    GUILayout.Space(10f);
                    GUILayout.EndHorizontal();
                    if (FsmEditorSettings.DebugVariables)
                    {
                        FsmEditorGUILayout.ReadonlyTextField(variable.ToString(), new GUILayoutOption[0]);
                    }
                }
            }
        }

        private void DoTableHeaders()
        {
            EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
            bool changed = GUI.changed;
            GUI.changed = false;
            GUILayout.BeginHorizontal(FsmEditorStyles.TableRowBox, new GUILayoutOption[0]);
            this.sortByVariableType = !GUILayout.Toggle(!this.sortByVariableType, new GUIContent("Name", "Right-click Variable to see States that use the Variable."), FsmEditorStyles.TableRowHeader, new GUILayoutOption[] { GUILayout.MinWidth(155f) });
            GUILayout.Label(new GUIContent("Used", "The number of times the Variable is used in the FSM."), new GUILayoutOption[0]);
            GUILayout.Space(10f);
            this.sortByVariableType = GUILayout.Toggle(this.sortByVariableType, new GUIContent("Type", "View/Edit the Variable's type."), FsmEditorStyles.TableRowHeader, new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            if (FsmEditorGUILayout.SettingsButtonPadded())
            {
                this.GenerateVariableManagerMenu().ShowAsContext();
            }
            GUILayout.EndHorizontal();
            if (GUI.changed)
            {
                this.SortVariableList();
            }
            else
            {
                GUI.changed = changed;
            }
            EditorGUILayout.EndHorizontal();
        }

        private void DoVariableContextMenu(int variableIndex)
        {
            if ((variableIndex >= 0) && (variableIndex <= (this.fsmVariables.Count - 1)))
            {
                FsmVariable variable = this.fsmVariables[variableIndex];
                this.GenerateStateList(variable.NamedVar).ShowAsContext();
            }
        }

        private void DoVariableEditor()
        {
            EditorGUIUtility.LookLikeControls(100f);
            FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
            EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.newVariableName = EditorGUILayout.TextField(string.IsNullOrEmpty(this.selectedVariableName) ? new GUIContent("New Variable", "Add a Variable to this FSM. Variables are used by Actions to store information") : new GUIContent("Edit Variable", "Edit the selected Variable."), this.newVariableName, new GUILayoutOption[0]);
            bool flag = this.IsValidVariableName(this.selectedVariableName, this.newVariableName);
            EditorGUILayout.EndHorizontal();
            FsmVariable fsmVariable = this.GetVariable(this.selectedVariableName);
            if (fsmVariable != null)
            {
                bool changed = GUI.changed;
                GUI.changed = false;
                fsmVariable.DoValueGUI(null, false);
                fsmVariable.Tooltip = EditorGUILayout.TextField(new GUIContent("Tooltip", "Tooltip used in Inspector and Action Editor. Not supported for Vector3."), fsmVariable.Tooltip, new GUILayoutOption[0]);
                fsmVariable.ShowInInspector = EditorGUILayout.Toggle(new GUIContent("Inspector", "Show this variable in the PlayMakerFSM Inspector"), fsmVariable.ShowInInspector, new GUILayoutOption[0]);
                if (FsmVariable.CanNetworkSync(fsmVariable.NamedVar))
                {
                    fsmVariable.NetworkSync = EditorGUILayout.Toggle(new GUIContent("Network Sync", "Sync this variable over the Network. NOTE: Requires a NetworkView component on the GameObject that Observes this PlayMakerFSM."), fsmVariable.NetworkSync, new GUILayoutOption[0]);
                    if (fsmVariable.NetworkSync)
                    {
                        this.CheckForNetworkSyncErrors();
                    }
                }
                else
                {
                    bool enabled = GUI.enabled;
                    GUI.enabled = false;
                    EditorGUILayout.Toggle(new GUIContent("Network Sync", "Network Sync is not available for this variable type."), false, new GUILayoutOption[0]);
                    GUI.enabled = enabled;
                }
                if (GUI.changed)
                {
                    this.UpdateVariable(fsmVariable, "");
                }
                else
                {
                    GUI.changed = changed;
                }
            }
            else
            {
                GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                this.newVariableType = (VariableType) EditorGUILayout.Popup(new GUIContent("Variable Type", "Select the type of information stored. Rollover Action parameters to see the type of each parameter."), (int) this.newVariableType, FsmVariable.VariableTypeNames, new GUILayoutOption[0]);
                bool flag4 = GUI.enabled;
                GUI.enabled = (GUI.enabled && flag) && (this.newVariableName.Length > 0);
                if (GUILayout.Button(new GUIContent("Add"), new GUILayoutOption[] { GUILayout.MaxWidth(35f), GUILayout.MaxHeight(15f) }))
                {
                    AddVariable(this.newVariableType, this.newVariableName, true);
                    if (FsmEditorSettings.SelectNewVariables)
                    {
                        this.SelectVariable(this.newVariableName);
                    }
                    this.Reset();
                }
                GUI.enabled = flag4;
                GUILayout.EndHorizontal();
            }
            if (Event.current.isKey)
            {
                if (flag && FsmEditorUtility.EnterKeyPressed())
                {
                    if (string.IsNullOrEmpty(this.selectedVariableName))
                    {
                        AddVariable(this.newVariableType, this.newVariableName, true);
                        if (FsmEditorSettings.SelectNewVariables)
                        {
                            this.SelectVariable(this.newVariableName);
                        }
                    }
                    else
                    {
                        this.RenameVariable(this.selectedVariableName, this.newVariableName);
                    }
                    this.Reset();
                    Event.current.Use();
                }
                if (Event.current.keyCode == KeyCode.Escape)
                {
                    this.Reset();
                }
            }
            if (!flag && !string.IsNullOrEmpty(this.newVariableName))
            {
                GUILayout.Box("Variable name already used!", FsmEditorStyles.ErrorBox, new GUILayoutOption[0]);
            }
            if (FsmEditorSettings.ShowHints)
            {
                GUILayout.Box(!string.IsNullOrEmpty(this.selectedVariableName) ? "Expose Variables and Events in the PlayMaker FSM Inspector to build a custom control panel for an FSM." : "Make Variables to use in Actions. Hint: In the State panel, rollover an Action Parameter to see the type of variable required.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
            }
        }

        private void DoVariableTable()
        {
            this.scrollPosition = GUILayout.BeginScrollView(this.scrollPosition, new GUILayoutOption[0]);
            if (FsmEditorSettings.ShowHints)
            {
                GUILayout.Box("Right-click Variable to select States that use the Variable.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
            }
            if (this.fsmVariables.Count == 0)
            {
                GUILayout.BeginHorizontal(FsmEditorStyles.TableRowBox, new GUILayoutOption[0]);
                GUILayout.Label("[none]", new GUILayoutOption[0]);
                GUILayout.EndHorizontal();
            }
            for (int i = 0; i < this.fsmVariables.Count; i++)
            {
                int num2;
                FsmVariable fsmVariable = this.fsmVariables[i];
                bool flag = fsmVariable.Name == this.selectedVariableName;
                GUILayout.BeginVertical(new GUILayoutOption[0]);
                GUILayout.BeginHorizontal(flag ? FsmEditorStyles.SelectedEventBox : (FsmEditorSettings.DebugVariables ? FsmEditorStyles.TableRowBoxNoDivider : FsmEditorStyles.TableRowBox), new GUILayoutOption[0]);
                GUIStyle style = flag ? FsmEditorStyles.TableRowTextSelected : FsmEditorStyles.TableRowText;
                if (GUILayout.Button(new GUIContent(fsmVariable.Name, fsmVariable.Tooltip), style, new GUILayoutOption[] { GUILayout.MinWidth(155f) }))
                {
                    this.SelectVariable(fsmVariable.Name);
                    if ((Event.current.button == 1) || EditorGUI.actionKey)
                    {
                        this.GenerateStateList(fsmVariable.NamedVar).ShowAsContext();
                    }
                }
                this.usageCount.TryGetValue(fsmVariable.NamedVar, out num2);
                GUILayout.FlexibleSpace();
                GUILayout.Label(num2.ToString(CultureInfo.CurrentCulture), style, new GUILayoutOption[0]);
                GUILayout.Space(10f);
                bool changed = GUI.changed;
                GUI.changed = false;
                VariableType newType = (VariableType) EditorGUILayout.Popup((int) fsmVariable.Type, FsmVariable.VariableTypeNames, new GUILayoutOption[] { GUILayout.MaxWidth(114f) });
                if (GUI.changed)
                {
                    this.ChangeVariableType(fsmVariable, newType);
                    GUIUtility.ExitGUI();
                    return;
                }
                GUI.changed = changed;
                if (FsmEditorGUILayout.DeleteButton())
                {
                    this.DeleteVariable(fsmVariable, true, true);
                    GUIUtility.ExitGUI();
                    return;
                }
                GUILayout.EndHorizontal();
                if (FsmEditorSettings.DebugVariables)
                {
                    FsmEditorGUILayout.ReadonlyTextField(fsmVariable.NamedVar.ToString(), new GUILayoutOption[0]);
                }
                GUILayout.EndVertical();
                if (flag)
                {
                    this.selectedIndex = i;
                    if (Event.current.type == EventType.Repaint)
                    {
                        selectedRect = GUILayoutUtility.GetLastRect();
                        selectedRect.y -= this.scrollPosition.y;
                    }
                }
            }
            this.DoGlobalVariablesTable();
            GUILayout.Space(20f);
            GUILayout.EndScrollView();
            this.DoAutoScroll();
            GUILayout.FlexibleSpace();
        }

        private GenericMenu GenerateStateList(NamedVariable variable)
        {
            int num;
            List<FsmInfo> list;
            this.UpdateUseCount();
            GenericMenu menu = new GenericMenu();
            this.usageCount.TryGetValue(variable, out num);
            if (num == 0)
            {
                menu.AddDisabledItem(new GUIContent("No states use this variable..."));
                return menu;
            }
            this.usage.TryGetValue(variable, out list);
            foreach (FsmInfo info in list)
            {
                menu.AddItem(new GUIContent(info.state.Name), FsmEditor.SelectedState == info.state, new GenericMenu.MenuFunction2(VariableManager.SelectState), info.state);
            }
            return menu;
        }

        private GenericMenu GenerateVariableManagerMenu()
        {
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("Debug Variable Values"), FsmEditorSettings.DebugVariables, new GenericMenu.MenuFunction(EditorCommands.ToggleDebugVariables));
            menu.AddItem(new GUIContent("Delete Unused Variables"), false, new GenericMenu.MenuFunction(this.DeleteUnusedVariables));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Global Variables"), false, new GenericMenu.MenuFunction(FsmEditor.OpenGlobalVariablesWindow));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Online Help"), false, new GenericMenu.MenuFunction(VariableManager.OpenOnlineHelp));
            return menu;
        }

        private FsmVariable GetVariable(string name)
        {
            foreach (FsmVariable variable in this.fsmVariables)
            {
                if (variable.Name == name)
                {
                    return variable;
                }
            }
            return null;
        }

        public IEnumerable<string> GetVariablesUsedByStates(IEnumerable<FsmState> states)
        {
            this.BuildFsmVariableList();
            this.UpdateUseCount();
            List<string> list = new List<string>();
            foreach (FsmVariable variable in this.fsmVariables)
            {
                foreach (FsmState state in states)
                {
                    if (this.StateUsesVariable(state, variable.NamedVar))
                    {
                        list.Add(variable.Name);
                        break;
                    }
                }
            }
            return list;
        }

        private bool IsValidVariableName(string fsmVariableName, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return false;
            }
            FsmVariable variable = this.GetVariable(fsmVariableName);
            foreach (FsmVariable variable2 in this.fsmVariables)
            {
                if ((variable != variable2) && (name == variable2.Name))
                {
                    return false;
                }
            }
            return true;
        }

        private void KeyboardGUI()
        {
            if (GUIUtility.keyboardControl == 0)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Keyboard);
                if (Event.current.GetTypeForControl(controlID) == EventType.KeyDown)
                {
                    switch (Event.current.keyCode)
                    {
                        case KeyCode.UpArrow:
                            Event.current.Use();
                            this.SelectPrevious();
                            GUIUtility.ExitGUI();
                            return;

                        case KeyCode.DownArrow:
                            Event.current.Use();
                            this.SelectNext();
                            GUIUtility.ExitGUI();
                            return;

                        case KeyCode.Return:
                            Event.current.Use();
                            if (GUIUtility.keyboardControl == 0)
                            {
                                this.Deselect();
                            }
                            GUIUtility.ExitGUI();
                            return;
                    }
                }
            }
        }

        public void OnGUI()
        {
            if (FsmEditor.SelectedFsm == null)
            {
                GUILayout.FlexibleSpace();
            }
            else
            {
                if (this.listIsDirty)
                {
                    this.BuildFsmVariableList();
                    this.UpdateUseCount();
                }
                this.KeyboardGUI();
                this.DoTableHeaders();
                this.DoVariableTable();
                if (Event.current.type == EventType.ContextClick)
                {
                    this.GenerateVariableManagerMenu().ShowAsContext();
                }
                this.DoVariableEditor();
                EditorGUILayout.Space();
                if (GUILayout.Button(new GUIContent("Global Variables", "Use the Global Variables window to manage global variables that can be seen by any FSM."), new GUILayoutOption[0]))
                {
                    FsmEditor.OpenGlobalVariablesWindow();
                    GUIUtility.ExitGUI();
                }
                if (FsmEditorSettings.ShowHints)
                {
                    GUILayout.Box("Use the Globals Variables Window to manage variables that can be seen by any FSM.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                }
                EditorGUILayout.Space();
                if ((Event.current.type == EventType.MouseDown) && (GUIUtility.keyboardControl == 0))
                {
                    this.Deselect();
                }
            }
        }

        private static void OpenOnlineHelp()
        {
            EditorCommands.OpenWikiPage(WikiPages.VariableManager);
        }

        private void RenameVariable(string fsmVariableName, string newName)
        {
            FsmVariable fsmVariable = this.GetVariable(fsmVariableName);
            if (fsmVariable != null)
            {
                FsmEditor.RegisterUndo("Edit Variable");
                this.UpdateVariable(fsmVariable, newName);
                FsmEditor.SaveActions();
                FsmEditor.SelectedFsm.InitData();
            }
        }

        public void Reset()
        {
            FsmEditor.ResetKeyboard();
            this.UpdateGlobalVariablesInfo();
            this.BuildFsmVariableList();
            this.UpdateUseCount();
            this.SanityCheckSelection();
            FsmEditor.Repaint(true);
            FsmEditor.RepaintAll();
        }

        public void SanityCheckSelection()
        {
            foreach (FsmVariable variable in this.fsmVariables)
            {
                if (variable.Name == this.selectedVariableName)
                {
                    return;
                }
            }
            this.Deselect();
        }

        private void SelectFirst()
        {
            this.SelectVariable(0);
        }

        private void SelectLast()
        {
            this.SelectVariable((int) (this.fsmVariables.Count - 1));
        }

        private void SelectNext()
        {
            if (this.selectedIndex == -1)
            {
                this.SelectLast();
            }
            else
            {
                this.SelectVariable((int) (this.selectedIndex + 1));
            }
        }

        private void SelectPrevious()
        {
            if (this.selectedIndex == -1)
            {
                this.SelectFirst();
            }
            else
            {
                this.SelectVariable((int) (this.selectedIndex - 1));
            }
        }

        private static void SelectState(object userdata)
        {
            EditorCommands.SelectState(userdata);
            FsmEditor.Inspector.SetMode(HutongGames.PlayMakerEditor.InspectorMode.StateInspector);
        }

        private void SelectVariable(int index)
        {
            if ((index >= 0) && (index <= (this.fsmVariables.Count - 1)))
            {
                this.SelectVariable(this.fsmVariables[index].Name);
            }
        }

        public void SelectVariable(string variableName)
        {
            FsmEditor.ResetKeyboard();
            this.selectedVariableName = variableName;
            this.newVariableName = this.selectedVariableName;
            autoScroll = true;
        }

        private void SortVariableList()
        {
            if (this.sortByVariableType)
            {
                this.fsmVariables.Sort(new Comparison<FsmVariable>(FsmVariable.CompareByType));
            }
            else
            {
                this.fsmVariables.Sort();
            }
        }

        public bool StateUsesVariable(FsmState state, NamedVariable variable)
        {
            List<FsmInfo> list;
            this.usage.TryGetValue(variable, out list);
            foreach (FsmInfo info in list)
            {
                if (info.state == state)
                {
                    return true;
                }
            }
            return false;
        }

        private void UpdateGlobalVariablesInfo()
        {
            this.globalVariablesInfo = FsmInfo.FindGlobalVariablesUsage(FsmEditor.SelectedFsm);
            this.globalVariables = FsmInfo.GetVariableList(this.globalVariablesInfo);
        }

        public void UpdateUseCount()
        {
            this.usage.Clear();
            this.usageCount.Clear();
            this.unusedVariables.Clear();
            foreach (FsmVariable variable in this.fsmVariables)
            {
                List<FsmInfo> list = FsmInfo.FindVariableUsage(variable.NamedVar);
                this.usage.Add(variable.NamedVar, list);
                this.usageCount.Add(variable.NamedVar, list.Count);
                if (list.Count == 0)
                {
                    this.unusedVariables.Add(variable);
                }
            }
            foreach (NamedVariable variable2 in this.globalVariables)
            {
                List<FsmInfo> list2 = FsmInfo.FindVariableUsage(variable2);
                this.usage.Add(variable2, list2);
                this.usageCount.Add(variable2, list2.Count);
            }
        }

        private void UpdateVariable(FsmVariable fsmVariable, [Optional, DefaultParameterValue("")] string newName)
        {
            FsmEditor.RegisterUndo("Edit Variable");
            if (!string.IsNullOrEmpty(newName))
            {
                fsmVariable.Name = newName;
            }
            this.listIsDirty = true;
            FsmEditor.SetFsmDirty(true, false);
        }

        public void UpdateView()
        {
            this.BuildFsmVariableList();
            FsmEditor.Repaint(false);
        }

        public bool VariableNameExists(string name)
        {
            foreach (FsmVariable variable in this.fsmVariables)
            {
                if (name == variable.Name)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

